<?php

namespace app\api\model\question;

use app\api\model\job\Job;
use app\api\model\user\Application;
use app\common\model\questionnaire\Questionnaire as Model;
use app\common\model\store\Level;

class Questionnaire extends Model
{
//    public function getTestInfo($level_id)
//    {
//        $levelInfo = Level::detail($level_id);
//        if (!$levelInfo) {
//            $this->error = "等级信息错误";
//            return false;
//        }
//        if (!$levelInfo->question_id) {
//            $this->error = "系统升级中";
//            return false;
//        }
////        $topicInfo = self::detail($levelInfo->question_id);
////        if (!$topicInfo || $topicInfo->status == 1) {
////            $this->error = "题库升级中";
////            return false;
////        }
//        $topicInfo = $this->where([
//            'question_id' => $levelInfo->question_id,
//            'status' => 0,
//            'is_delete' => 0,
//        ])->find();
//        if (!$topicInfo) {
//            $this->error = "题库升级中";
//            return false;
//        }
//
//        $topicModel = (new Topic());
//        $radio_num = $topicModel->byTypeCount($topicInfo->question_id, 2);
//        $multiple_num = $topicModel->byTypeCount($topicInfo->question_id, 3);
//        $judge_num = $topicModel->byTypeCount($topicInfo->question_id, 1);
//
//        if (($radio_num + $judge_num + $multiple_num) < 50) {
//            $this->error = "题目不足考试";
//            return false;
//        }
//
//        $radio = [];
//        $multiple = [];
//        $judge = [];
//        if ($radio_num >= 30 && $judge_num >= 10 && $multiple_num >= 10) {
//            $radio = $topicModel->byTypeRandList($topicInfo->question_id, 2, 30);
//            $multiple = $topicModel->byTypeRandList($topicInfo->question_id, 3, 10);
//            $judge = $topicModel->byTypeRandList($topicInfo->question_id, 1, 10);
//        } else {
//            $list = $topicModel
//                ->where([
//                    'question_id' => $topicInfo->question_id,
//                    'is_delete' => 0,
//                    'status' => 0,
//                ])
//                ->limit(50)
//                ->orderRaw('rand()')
//                ->select()
//                ->toArray();
//            foreach ($list as $item) {
//                if ($item['qt_type'] == 1) {
//                    $judge[] = $item;
//                } else if ($item['qt_type'] == 2) {
//                    $radio[] = $item;
//                } else {
//                    $multiple[] = $item;
//                }
//            }
//        }
//        shuffle($radio);
//        shuffle($multiple);
//        shuffle($judge);
//        $item_list = array_merge($radio, $judge, $multiple);
//        return ['item' => $item_list, 'topic' => $topicInfo];
//    }
//
//    public function getPracticeList($application_id)
//    {
//        $application = Application::detail($application_id);
//        if (!$application) {
//            return false;
//        }
//        $subject = Job::detail($application->subject_id);
//        if (!$subject) {
//            throwError('未找到指定工种');
//        }
//        $subject_level = Level::detail($application->subject_level_id);
//        if (!$subject_level || !$subject_level->question_id) {
//            throwError('未找到指定工种等级/未关联题库');
//        }
//        $question = $this->where([
//            'question_id' => $subject_level->question_id,
//            'status' => 0,
//            'is_delete' => 0,
//        ])->find();
//        if (!$question) {
//            throwError('题库升级中');
//        }
//
//        $qt_list = (new Topic())->where([
//            'question_id' => $subject_level->question_id,
//            'status' => 0,
//            'is_delete' => 0
//        ])->orderRaw('rand()')->select()->toArray();
//        return ['application' => $application, 'qt_list' => $qt_list, 'job' => $subject, 'subject_level' => $subject_level];
//    }

    public function recordLeam($application_id = '')
    {
        $application = Application::detail($application_id);
        if (!$application) {
            return false;
        }
        $application->accumulate_learn = $application->accumulate_learn + 1;
        $application->save();
        return $application->accumulate_learn;
    }

    public function getObtainPracticeQuestions($student_sign_id)
    {

    }
}
